import 'package:flutter/material.dart';
import 'models/product.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'dart:convert' as convert;
import 'components/product_widget.dart';
import 'pages/prouct_detail.dart';
import 'package:request/request.dart' as http;



void main() => runApp(MyApp());

// #docregion MyApp
class MyApp extends StatelessWidget {
  // #docregion build
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Startup Name Generator',
      home: Shop(),
    );
  }
// #enddocregion build
}

class Shop extends StatefulWidget {
  @override
  _ShopState createState() => _ShopState();
}

class _ShopState extends State<Shop> {
  var products = <Product>[];
  @override
  void initState() {
    super.initState();
    rootBundle.loadString('lib/resource/plist/products.json').then((value){
      var maps = convert.jsonDecode(value);
      var products = maps.map<Product>((item)=>Product(item['name'], item['price'], item['url'])).toList();
      setState(() {
        this.products = products;
      });
    });


    //测试网络请求：
    http.get(Uri.parse("https://www.baidu.com")).then((value){
      print(value);
    });

  }


  @override
  Widget build(BuildContext context) {
    var productChildren = products.map<Widget>((item)=>GestureDetector(
      child: ProductWidget(item),
      onTap: (){
        Navigator.of(context).push(
          MaterialPageRoute<void>(
            // NEW lines from here...
            builder: (BuildContext context) {
              return ProductDetail(item,(callBackValue){
                print('回调:');
                print(callBackValue);
              });
            }, // ...to here.
          ),
        );
      }),
    ).toList();
    return Scaffold(
      appBar: AppBar(title: Text("商城"),),
      body: ListView(
        children: productChildren,
        semanticChildCount: productChildren.length,
      ),
    );
  }
}
